﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SchoolManagementSystem.Controls;
using CoreData;
using Helper;
using DevExpress.XtraEditors.Controls;
namespace SchoolManagementSystem.Details
{
    public partial class Tracking_Student_FeeUC : DetailUC
    {
        KList<TypeOfFee> TypeOfFeeLst = new KList<TypeOfFee>();
        public Tracking_Student_FeeUC()
        {
            InitializeComponent();
            this.Code = "Student_Fee";
            this.ObjectName = "theo dõi học phí";
            this.Type = new Student_Fee();

            TypeOfFeeLst.Add(new TypeOfFee() { ID=1,Name="Đóng một lần" });
            TypeOfFeeLst.Add(new TypeOfFee() { ID = 2, Name = "Đóng hai lần" });
            TypeOfFeeLst.Add(new TypeOfFee() { ID = 3, Name = "Đóng ba lần" });
            TypeOfFeeLst.Add(new TypeOfFee() { ID = 4, Name = "Đóng bốn lần" });
            TypeOfFeeLst.Add(new TypeOfFee() { ID = 6, Name = "Đóng sáu lần" });
            LoadTypeOfFee();
            txtclass.Enabled = false;
            txtbranch.Enabled = false;
            txtcode.Enabled = false;
            txtgvcn.Enabled = false;
            txtname.Enabled = false;
            txtcourse.Enabled = false;
            comboBoxEdit_Hinhthuc.Enabled = false;

            masterMainUC1.Model = RegisterService.RegisterServices.ReceiptVoucherFeeService;
            masterMainUC1.DetailUCControl = new Tracking_Student_Fee_DetailUC();

        }

       

        void LoadTypeOfFee()
        {
            try
            {
               //// DataTable data = TypeOfFeeLst.ConvertToDataTable(TypeOfFeeLst);
               // var data = TypeOfFeeLst.ToArray();
               // lookUpEdit_hinhthucdong.Properties.Columns.Add(new LookUpColumnInfo("Name"));
               // lookUpEdit_hinhthucdong.Properties.DataSource = (object)data;
               // lookUpEdit_hinhthucdong.Properties.ValueMember = "ID";
               // lookUpEdit_hinhthucdong.Properties.DisplayMember = "Name";
               // lookUpEdit_hinhthucdong.Properties.PopulateColumns();
               // lookUpEdit_hinhthucdong.Properties.Columns["ID"].Visible = false;
               
               
               
               // comboBoxEdit_Hinhthuc.();

                ComboBoxItemCollection coll = comboBoxEdit_Hinhthuc.Properties.Items;
                coll.BeginUpdate();
                try
                {
                    comboBoxEdit_Hinhthuc.Properties.NullText = "[Rỗng]";
                    comboBoxEdit_Hinhthuc.Properties.Items.AddRange(TypeOfFeeLst);
                }
                finally
                {
                    coll.EndUpdate();
                }
                comboBoxEdit_Hinhthuc.SelectedIndex = -1;

               
            }
            catch { }
        }

        String StudentID = "1=1";
        protected override long DetailDisplay()
        {
         //  LoadTypeOfFee();
            commandBarUC1.cmdnew.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            Student_Fee data = new Student_Fee();
            if (ID > 0)
            {
                data = (Student_Fee)RegisterService.RegisterServices.Student_FeeService.GetByID(ID);
                StudentID = data.StudentID.ToString();// ID.ToString(); 
            }
            txtcode.Text = data.StudentCodeExt;
            txtname.Text = data.StudentNameExt;
            txtnote.Text = data.Description;
            lblfeetopay.Text = Helper.StringHelper.FormatVND(data.FeeToPayExt);
            txtbranch.Text = data.BranchNameExt;
            txtclass.Text = data.ClassNameExt;
            txtcourse.Text = data.CourseNameExt;
            txtgvcn.Text = data.TeacherNameExt;
            try
            {
                comboBoxEdit_Hinhthuc.SelectedItem = TypeOfFeeLst.First(c => c.ID == data.TypeExt); //
            }
            catch { }
            spinEdit_price.EditValue = data.Money;
            comboBoxEdit_status.SelectedIndex = data.Status; //0 đóng đủ,  1 chưa đủ, 2 chua dong
            if (ID > 0)
            {
                SetDataFeeTime(data.TypeExt,data.FeeToPayExt);
            }
            masterMainUC1.SearchString = " StudentID='" + StudentID + "'";
            masterMainUC1.DetailUCControl.SearchString = " StudentID='" + StudentID + "'";
            return ID;

        }

        protected override long Save()
        {
            commandBarUC1.cmdnew.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            Student_Fee data = new Student_Fee();
            if (ID > 0)
            {
                data = (Student_Fee)RegisterService.RegisterServices.Student_FeeService.GetByID(ID);
            }
            data.Status = comboBoxEdit_status.SelectedIndex;
            data.Money = int.Parse(spinEdit_price.EditValue.ToString());
          //  data.Type = int.Parse(((TypeOfFee)comboBoxEdit_Hinhthuc.SelectedItem).ID.ToString());
            data.Description = txtnote.Text;

            ID = RegisterService.RegisterServices.Student_FeeService.Save(data);

            //update lại bảng số tiền đóng theo kỳ
            Student_FeeTime st = RegisterService.RegisterServices.StudentFeeTimeService.GetByStudentID(data.StudentID);

            RegisterService.RegisterServices.StudentService.SaveStudentFeeTime(data.StudentID, data.TypeExt, 0,0, DateTime.MinValue, DateTime.MinValue,
               dateEdit_start1.DateTime, dateEdit_end1.DateTime, dateEdit_start2.DateTime, dateEdit_end2.DateTime
               , dateEdit_start3.DateTime, dateEdit_end3.DateTime, dateEdit_start4.DateTime, dateEdit_end4.DateTime
               , dateEdit_start5.DateTime, dateEdit_end5.DateTime, dateEdit_start6.DateTime, dateEdit_end6.DateTime,
               int.Parse(spinEdit_Money1.EditValue.ToString()), int.Parse(spinEdit_Money2.EditValue.ToString()), int.Parse(spinEdit_Money3.EditValue.ToString())
               , int.Parse(spinEdit_Money4.EditValue.ToString()), int.Parse(spinEdit_Money5.EditValue.ToString()),
               int.Parse(spinEdit_Money6.EditValue.ToString()));

            masterMainUC1.DetailUCControl.SearchString = ID.ToString(); 

            return ID;
        }

     


        public override bool Delete(long SelectedID)
        {

            return RegisterService.RegisterServices.Student_FeeService.Delete(RegisterService.RegisterServices.Student_FeeService.GetByID(SelectedID));

        }

     
        private void Tracking_Student_FeeUC_Load_1(object sender, EventArgs e)
        {
            AddTextChanged(xtraTabPage1.Controls);
            AddTextChanged(xtraTabPage2.Controls);
            AddTextChanged(groupControl_time1.Controls);
            AddTextChanged(groupControl_time2.Controls);
            AddTextChanged(groupControl_time3.Controls);
            AddTextChanged(groupControl_time4.Controls);
            AddTextChanged(groupControl_time5.Controls);
            AddTextChanged(groupControl_time6.Controls);
          
        }



        void SetDataFeeTime(int type, int totalfee)
        {
            var data  = (Student_FeeTime)RegisterService.RegisterServices.StudentFeeTimeService.GetStudentFeeTimeByStudentIDAndType(long.Parse(StudentID),type);
            if (type == 1)
            {
                groupControl_time2.Visible = false;
                groupControl_time3.Visible = false;
                groupControl_time4.Visible = false;
                groupControl_time5.Visible = false;
                groupControl_time6.Visible = false;
                groupControl_time1.Visible = true;
              //  data.Money1 = totalfee;

            }
            if (type == 2)
            {
                groupControl_time3.Visible = false;
                groupControl_time4.Visible = false;
                groupControl_time6.Visible = false;
                groupControl_time5.Visible = false;
                groupControl_time1.Visible = true;
                groupControl_time2.Visible = true;
               // data.Money1 = totalfee / 2;
              //  data.Money2 = totalfee / 2;

            }
            if (type == 3)
            {
                groupControl_time4.Visible = false;
                groupControl_time6.Visible = false;
                groupControl_time5.Visible = false;
                groupControl_time1.Visible = true;
                groupControl_time2.Visible = true;
                groupControl_time3.Visible = true;
               // data.Money1 = totalfee /3;
              //  data.Money2 = totalfee /3;
               // data.Money3 = totalfee / 3;
            }
            if (type == 4)
            {
                groupControl_time6.Visible = false;
                groupControl_time5.Visible = false;
                groupControl_time1.Visible = true;
                groupControl_time2.Visible = true;
                groupControl_time3.Visible = true;
                groupControl_time4.Visible = true;
               // data.Money1 = totalfee / 4;
               // data.Money2 = totalfee / 4;
              //  data.Money3 = totalfee / 4;
             //   data.Money4 = totalfee / 4;
            }
            if (type == 6)
            {
                groupControl_time1.Visible = true;
                groupControl_time2.Visible = true;
                groupControl_time3.Visible = true;
                groupControl_time4.Visible = true;
                groupControl_time6.Visible = true;
                groupControl_time5.Visible = true;
               // data.Money1 = totalfee / 4;
               // data.Money2 = totalfee / 4;
               // data.Money3 = totalfee / 4;
               // data.Money4 = totalfee / 4;
               // data.Money5 = totalfee / 4;
             //   data.Money6 = totalfee / 4;
            }

            dateEdit_start1.DateTime = data.StartDate1;
            dateEdit_start2.DateTime = data.StartDate2;
            dateEdit_start3.DateTime = data.StartDate3;
            dateEdit_start4.DateTime = data.StartDate4;
            dateEdit_start5.DateTime = data.StartDate5;
            dateEdit_start6.DateTime = data.StartDate6;

            dateEdit_end1.DateTime = data.EndDate1;
            dateEdit_end2.DateTime = data.EndDate2;
            dateEdit_end3.DateTime = data.EndDate3;
            dateEdit_end4.DateTime = data.EndDate4;
            dateEdit_end5.DateTime = data.EndDate5;
            dateEdit_end6.DateTime = data.EndDate6;

            spinEdit_Money1.EditValue = data.Money1;
            spinEdit_Money2.EditValue = data.Money2;
            spinEdit_Money3.EditValue = data.Money3;
            spinEdit_Money4.EditValue = data.Money4;
            spinEdit_Money5.EditValue = data.Money5;
            spinEdit_Money6.EditValue = data.Money6;
        }

    }
    //hinh thuc dong hoc phi
    class TypeOfFee
    {
        public int ID;
        public string Name;
        public override string ToString()
        {
            return ID + "     " + Name;
        }
    }

}
